package com.tourism.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tourism.common.Result;
import com.tourism.entity.ActivityBooking;
import com.tourism.mapper.ActivityBookingMapper;
import com.tourism.service.ActivityBookingService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class ActivityBookingServiceImpl extends ServiceImpl<ActivityBookingMapper, ActivityBooking> implements ActivityBookingService {

    @Override
    public Result getBookingList(Integer page, Integer pageSize, String userName, String activityName) {
        Page<ActivityBooking> pageParam = new Page<>(page, pageSize);
        LambdaQueryWrapper<ActivityBooking> wrapper = new LambdaQueryWrapper<>();

        if (userName != null && !userName.isEmpty()) {
            wrapper.like(ActivityBooking::getUserName, userName);
        }
        if (activityName != null && !activityName.isEmpty()) {
            wrapper.like(ActivityBooking::getActivityName, activityName);
        }

        IPage<ActivityBooking> pageResult = page(pageParam, wrapper);
        return Result.success(pageResult);
    }

    @Override
    public Result getBookingById(Integer id) {
        if (id == null) {
            return Result.fail("预订ID不能为空");
        }
        ActivityBooking booking = getById(id);
        return booking != null ? Result.success(booking) : Result.fail("预订不存在");
    }

    @Override
    public Result addBooking(ActivityBooking activityBooking) {
        if (activityBooking.getUserId() == null) {
            return Result.fail("用户ID不能为空");
        }
        if (activityBooking.getActivityName() == null || activityBooking.getActivityName().isEmpty()) {
            return Result.fail("活动名称不能为空");
        }
        if (activityBooking.getBookingTime() == null) {
            return Result.fail("预订时间不能为空");
        }

        boolean success = save(activityBooking);
        return success ? Result.success("添加成功") : Result.fail("添加失败");
    }

    @Override
    public Result updateBooking(ActivityBooking activityBooking) {
        if (activityBooking.getId() == null) {
            return Result.fail("预订ID不能为空");
        }

        boolean success = updateById(activityBooking);
        return success ? Result.success("更新成功") : Result.fail("更新失败");
    }

    @Override
    public Result deleteBooking(Integer id) {
        if (id == null) {
            return Result.fail("预订ID不能为空");
        }

        boolean success = removeById(id);
        return success ? Result.success("删除成功") : Result.fail("删除失败");
    }

    @Override
    @Transactional
    public Result batchDeleteBookings(List<Integer> ids) {
        if (ids == null || ids.isEmpty()) {
            return Result.fail("预订ID列表不能为空");
        }

        boolean success = removeBatchByIds(ids);
        return success ? Result.success("批量删除成功") : Result.fail("批量删除失败");
    }
}
